//
//  PPTimelineDetailCommentView.m
//  pickpic
//
//  Created by Jinxiao on 10/4/16.
//  Copyright © 2016 MicroDream. All rights reserved.
//

#import "PPTimelineDetailCommentView.h"

@interface PPTimelineDetailCommentView () <UITextFieldDelegate>
@property (readwrite, nonatomic, strong) UITextField *textField;
@property (readwrite, nonatomic, strong) UIButton *sendButton;
@end

@implementation PPTimelineDetailCommentView

- (instancetype)init {
    self = [super init];

    self.backgroundColor = [UIColor colorWithRGBHex:0xf2f2f2];

    _textField = [[UITextField alloc] init];
    _textField.backgroundColor = [UIColor whiteColor];
    _textField.enablesReturnKeyAutomatically = YES;

    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 8, 1)];
    leftView.backgroundColor = [UIColor clearColor];
    _textField.leftView = leftView;
    _textField.leftViewMode = UITextFieldViewModeAlways;

    UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 8, 1)];
    rightView.backgroundColor = [UIColor clearColor];
    _textField.rightView = rightView;
    _textField.rightViewMode = UITextFieldViewModeAlways;

    _textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"添加评论..." attributes:@{NSForegroundColorAttributeName: [UIColor colorWithRGBHex:0xb2b2b2], NSFontAttributeName: [UIFont systemFontOfSize:14]}];
    _textField.returnKeyType = UIReturnKeySend;
    _textField.layer.borderColor = [UIColor colorWithRGBHex:0xd9d9d9].CGColor;
    _textField.layer.borderWidth = kLineWidth;
    _textField.layer.masksToBounds = YES;
    _textField.layer.cornerRadius = 5;
    _textField.delegate = self;
    _textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    _textField.font = [UIFont systemFontOfSize:14];
    [self addSubview:_textField];

    _sendButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _sendButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [_sendButton setTitle:@"发送" forState:UIControlStateNormal];
    [_sendButton setTitleColor:[UIColor colorWithRGBHex:0x323232] forState:UIControlStateNormal];
    [_sendButton setBackgroundImage:[[UIImage imageWithColor:[UIColor colorWithRGBHex:0xffdd17]] stretchableImageByCenter] forState:UIControlStateNormal];
    [_sendButton setBackgroundImage:[[UIImage imageWithColor:[UIColor colorWithRGBHex:0xffdd17]] stretchableImageByCenter] forState:UIControlStateHighlighted];
    [_sendButton setBackgroundImage:[[UIImage imageWithColor:[UIColor colorWithRGBHex:0xcccccc]] stretchableImageByCenter] forState:UIControlStateDisabled];
    _sendButton.layer.borderColor = [UIColor colorWithRGBHex:0xd9d9d9].CGColor;
    _sendButton.layer.borderWidth = kLineWidth;
    _sendButton.layer.masksToBounds = YES;
    _sendButton.layer.cornerRadius = 5;
    [self addSubview:_sendButton];

    [self layoutSendButton];
    
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];

    _sendButton.width = 50;
    _sendButton.height = 30;
    _sendButton.right = self.width - 10;
    _sendButton.centerY = self.height/2;

    _textField.left = 10;
    _textField.height = 30;
    _textField.centerY = self.height/2;
    _textField.width = _sendButton.left - 5 - 10;
}

- (void)beginEdit {
    [_textField becomeFirstResponder];
}

#pragma mark - UITextFieldDelegate

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if([string isEqualToString:@"\n"]) {

        [_sendButton sendActionsForControlEvents:UIControlEventTouchUpInside];

        return NO;
    }

    NSString *text = [textField.text stringByReplacingCharactersInRange:range withString:string];

    if(text.length == 0) {
        _sendButton.enabled = NO;
    } else {
        _sendButton.enabled = YES;
    }

    return YES;
}

- (NSString *)text {
    return _textField.text;
}

- (void)willSend {

}

- (void)didSend {
    self.target = nil;
    self.targetCommentID = nil;

    _textField.text = nil;

    [_textField resignFirstResponder];

    [self layoutSendButton];
}

- (void)setTarget:(PPTimelineUserObject *)target {
    _target = target;

    if(target == nil) {
        _textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"添加评论..." attributes:@{NSForegroundColorAttributeName: [UIColor colorWithRGBHex:0xb2b2b2], NSFontAttributeName: [UIFont systemFontOfSize:14]}];
    } else {
        _textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"回复 %@:", target.name] attributes:@{NSForegroundColorAttributeName: [UIColor colorWithRGBHex:0xb2b2b2], NSFontAttributeName: [UIFont systemFontOfSize:14]}];
    }

    _textField.text = nil;

    [self layoutSendButton];
}

- (void)layoutSendButton {
    if(_textField.text.length == 0) {
        _sendButton.enabled = NO;
    } else {
        _sendButton.enabled = YES;
    }
}

@end
